package cn.tedu.antconlonysever.contract.service.impl;

import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.ServiceCode;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IContractRepository;

import cn.tedu.antconlonysever.contract.pojo.entity.Contract;
import cn.tedu.antconlonysever.contract.pojo.param.ContractAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.ContractUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractListVo;
import cn.tedu.antconlonysever.contract.pojo.vo.ContractStandardVO;
import cn.tedu.antconlonysever.contract.service.IContractService;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class ContractServiceImpl implements IContractService {

    @Value("${antcolony.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IContractRepository contractRepository;
    @Override
    public void addNew(ContractAddNewParam contractAddNewParam) {
        log.debug("开始处理【添加合同】的业务，参数：{}", contractAddNewParam);
        String title=contractAddNewParam.getTitle();
        int count = contractRepository.countTitle(title);
        log.debug("根据名称【{}】统计数量，结果：{}", title, count);
        if(count>0){
            String message = "添加合同失败，合同名称已经被占用！";
            log.warn(message);
           throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        // 将合同数据写入到数据库中
        Contract contract=new Contract();
        BeanUtils.copyProperties(contractAddNewParam,contract);

        contract.setGmtCreate(LocalDateTime.now());
        contract.setGmtModified(LocalDateTime.now());
        int row = contractRepository.insert(contract);
        if(row!=1){
            String message = "添加合同失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的合同数据插入到数据库，完成！");

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除合同】的业务，参数：{}", id);
        ContractStandardVO contract=contractRepository.getStandardById(id);
        if(contract==null){
            String message = "删除合同失败，合同ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int row = contractRepository.deleteById(id);
        if(row!=1){
            String message = "删除合同失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, ContractUpdateParam contractUpdateParam) {
        log.debug("开始处理【修改合同】的业务，ID：{}，新数据：{}", id, contractUpdateParam);
        ContractStandardVO queryResult = contractRepository.getStandardById(id);
        if(queryResult==null){
            String message = "修改合同失败，合同数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //检查合同标题是否被其他合同占用,如果被占用，则抛出异常

        int count = contractRepository.countNameAndNotTitle(id,contractUpdateParam.getTitle());
        if(count>0){
            String message = "修改合同失败，尝试修改的相册标题已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //执行修改
        Contract contract=new Contract();
        BeanUtils.copyProperties(contractUpdateParam,contract);
        contract.setId(id);
        int row = contractRepository.update(contract);
        if(row!=1){
            String message = "修改合同失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }



    @Override
    public ContractStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询合同详情】的业务，ID：{}", id);
        ContractStandardVO queryResult = contractRepository.getStandardById(id);
        if(queryResult==null){
            String message = "根据ID查询合同详情失败，合同数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }


    @Override
    public PageData<ContractListVo> list(Integer pageNum) {
        PageData<ContractListVo> pageDate = contractRepository.list(pageNum, defaultQueryPageSize);
        return pageDate;
    }

    @Override
    public PageData<ContractListVo> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询合同列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<ContractListVo> pageData =  contractRepository.list(pageNum, pageSize);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }
}
